/*	____________________________________________________________________________

                            P h i M P  C o m p i l e r
	____________________________________________________________________________  */

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
using namespace std;

                     // GLOBAL FILE STREAMS
ifstream sourceFile; // source program
ofstream outFile,    // object program
         listFile;   // compiler listing

#include "scan.h"   // scanner component definition 
#include "parse.h"	 // parser component definition
#include "code.h"   // code generator component definition

                     // GLOBAL COMPONENT OBJECTS
Scanner scan;        // scanner
Parser parse;        // parser
CodeGen code;        // code generator

string Date();
// Returns the current date.

string Time();
// Returns the current time.

void main()
{
	string sourceName, outName, listName;

	cout 
		<< "\n" 
		<< " P h i M P    C O M P I L E R    2 0 0 9\n"
		<< " _____________________________________\n" 
		<< endl;

	cout << " Source file (.pmp extension is assumed): ";
	getline(cin, sourceName);

   // Add appropriate extensions to file names.
	outName = sourceName + ".asm";
	listName = sourceName + ".lst";
	sourceName += ".pmp";

   // Open and initialize all files.
	sourceFile.open(sourceName.data());
	if (!sourceFile.is_open())
	{
		cout << "\n File not found. Compilation aborted!\n\n";
		cin.get();
		exit(1);
	}
	outFile.open(outName.data());
	listFile.open(listName.data());

	listFile
		<< "\n\n P H I M P    C O M P I L E R    L I S T I N G\n\n"
		<< "  Phil Napieralski / Mathew Faris / Pat Heffernan\n"
		<< "               Version 2009\n\n"
		<< " Generated code is SAM assembly language for\n"
		<< " the MACC2 virtual computer.\n"
		<< " ___________________________________________\n\n";
	listFile << ' ' + Date() << " at " << Time() << endl;
	listFile << " Source file: " << sourceName << endl << endl;
	listFile << " LINE #" << endl;

	parse.SystemGoal();

	cout << endl
		<< "\n A most EXQUISITE compile, my fine sir/madam.\n"
		<< "\n Object code was tenderly placed into " << outName << endl
		<< "\n Listing file was tenderly placed into " << listName << endl << endl; 

	cin.get();
}

string Date()
{
	const time_t current_time = time (NULL);
	char the_date[20];

	strftime (the_date, 20, "%B %d, %Y", localtime (&current_time));

	return the_date;
}

string Time()
{
	const time_t current_time = time (NULL);
	char the_time[10];


	strftime (the_time, 10, "%I:%M %p", localtime (&current_time));
	
	return the_time;
}

//string Date()
//{
//	const string MONTH[] = 
//		{ "January", "February", "March", "April", "May", "June", 
//		  "July", "August", "September", "October", "November", 
//		  "December" };
//
//	char theDate[10];
//	int moNumber;
//
//	_strdate_s(theDate);
//	string strDate(theDate, theDate+8);
//
//	moNumber = 10 * (theDate[0] - '0') + theDate[1] - '0' - 1;
//	return MONTH[moNumber] + ' '
//		+ ((strDate[3] == '0') ? strDate.substr(4,1) 
//		                       : strDate.substr(3,2)) 
//		+ ", 20" + strDate.substr(6,3);
//}
//
//string Time()
//{
//	const  string HOUR[] 
//		= { "12", "1", "2", "3", "4", "5", "6", 
//		    "7", "8", "9", "10", "11" };
//	char   theTime[10];
//	int    hrNumber;
//	string suffix;
//
//	_strtime_s(theTime);
//	string strTime(theTime, theTime+8);
//
//	hrNumber = 10 * (theTime[0] - '0') + theTime[1] - '0';
//	if (hrNumber < 12)
//		suffix = " A.M.";
//	else
//	{
//		suffix = " P.M.";
//		hrNumber -= 12;
//	}
//
//	return HOUR[hrNumber] + ':' + strTime.substr(3,2) + suffix;
//}